package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IUserService userService;

    /**
     * 关注或取关
     * @param followedId
     * @param isFollow
     * @return
     */
    @Override
    public Result follow(Long followedId, boolean isFollow) {
        Long userId = UserHolder.getUser().getId();
        String key = "follow:" + userId;

        if(isFollow){
            //关注
            Follow follow = new Follow();
            follow.setFollowUserId(followedId);
            follow.setUserId(userId);
            follow.setCreateTime(LocalDateTime.now());

            boolean success = this.save(follow);
            if(success){
                stringRedisTemplate.opsForSet().add(key,followedId.toString());
            }
        }else{
            //取关
            boolean success = this.remove(new QueryWrapper<Follow>()
                    .eq("user_id", userId).eq("follow_user_id", followedId));
            if(success){
                stringRedisTemplate.opsForSet().remove(key,followedId.toString());
            }
        }
        return Result.ok();
    }

    /**
     * 判断是否已经关注
     * @param followedId
     * @return
     */
    @Override
    public Result isFollow(int followedId) {
        Long userId = UserHolder.getUser().getId();
        String key = "follow:" + userId;
//        Long count = this.query().eq("user_id", userId).eq("follow_user_id", followedId).count();
        Boolean member = stringRedisTemplate.opsForSet().isMember(key, String.valueOf(followedId));
        return Result.ok(BooleanUtil.isTrue(member));
    }

    @Override
    public Result common(int id) {
        Long userId = UserHolder.getUser().getId();
        String key = "follow:" + userId;
        String key1 = "follow:" + id;
        Set<String> commonFollow = stringRedisTemplate.opsForSet().intersect(key, key1);
        List<UserDTO> userDTOS = commonFollow.stream().map(userService::getById).map(user ->
                BeanUtil.copyProperties(user, UserDTO.class)
        ).collect(Collectors.toList());
        return Result.ok(userDTOS);
    }
}
